package com.ljkj.cordial.chat.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lanjiu.lib.imclient.dto.GroupDto;
import com.lanjiu.lib.imclient.dto.user.RegisteredUserDto;
import com.lanjiu.lib.imclient.dto.user.TouristsUserDto;
import com.lanjiu.lib.imclient.user.UserPreferences;
import com.ljkj.cordial.chat.R;
import com.ljkj.cordial.chat.bean.SearchBean;
import com.ljkj.cordial.chat.dao.notify.FriendTable;
import com.ljkj.cordial.chat.dao.notify.GroupMemberTable;
import com.ljkj.cordial.chat.dao.notify.GroupTable;
import com.ljkj.cordial.chat.widget.PuzzleImageView;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: Panwei
 * @date: 2020/2/13 14:31
 */
public class SearchListAdapter extends RecyclerView.Adapter<SearchListAdapter.SearchListViewHolder> {

    public static final int ITEM_FRIEND = 0;
    public static final int ITEM_GROUP = 1;

    public static final int RELATION_FRIENDLY = 0;
    public static final int RELATION_SELF = 1;
    public static final int RELATION_UNFAMILIAR = 2;

    private List<ItemBean> mDataSet;
    private OnItemClickListener mOnItemClickListener;
    private LayoutInflater mLayoutInflater;

    public SearchListAdapter(Context context) {
        this.mDataSet = new ArrayList<>();
        this.mLayoutInflater = LayoutInflater.from(context);
    }

    public void setDataSet(List<ItemBean> dataSet) {
        this.mDataSet.clear();
        this.mDataSet.addAll(dataSet);
        notifyDataSetChanged();
    }

    public void clear() {
        this.mDataSet.clear();
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @NonNull
    @Override
    public SearchListViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View itemView = this.mLayoutInflater.inflate(R.layout.item_search_list, viewGroup, false);
        return new SearchListViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull SearchListViewHolder vh, int i) {
        ItemBean item = mDataSet.get(i);
        if (TextUtils.isEmpty(item.alias)) {
            vh.tvName.setText(item.name);
        } else {
            vh.tvName.setText(String.format("%s(%s)", item.alias, item.name));
        }
        vh.tvId.setText(item.exhibition);
        vh.tvBtn.setVisibility(RELATION_UNFAMILIAR == item.relation ? View.VISIBLE : View.GONE);
        if (item.itemType == ITEM_FRIEND) {
            vh.tvBtn.setText("加好友");
            if (!TextUtils.isEmpty(item.avatarPath)) {
                vh.ivAvatar.setFriendPath(item.avatarPath, item.avatarUrl);
            } else {
                if (!TextUtils.isEmpty(item.avatarUrl)) {
                    vh.ivAvatar.setFriendUrl(item.avatarUrl, item.id);
                } else {
                    vh.ivAvatar.setImageResource(R.mipmap.pic_user_avatar_default_58dp);
                }
            }
        } else {
            vh.tvBtn.setText("申请加群");
            if (!TextUtils.isEmpty(item.avatarPath)) {
                vh.ivAvatar.setGroupPaths(item.avatarPath);
            } else {
                if (!TextUtils.isEmpty(item.avatarUrl)) {
                    vh.ivAvatar.setImageBackGround(item.avatarUrl, item.id);
                } else {
                    vh.ivAvatar.setImageResource(R.mipmap.pic_group_avatar_default_58dp);
                }
            }
        }
        vh.tvBtn.setOnClickListener(v -> {
            if (mOnItemClickListener != null)
                mOnItemClickListener.onItemClick(vh.tvBtn, item, i);
        });
        vh.itemView.setOnClickListener(v -> {
            if (mOnItemClickListener != null)
                mOnItemClickListener.onItemClick(vh.itemView, item, i);
        });
    }

    @Override
    public int getItemCount() {
        return mDataSet.size();
    }

    class SearchListViewHolder extends RecyclerView.ViewHolder {

        PuzzleImageView ivAvatar;
        TextView tvName, tvId, tvBtn;

        SearchListViewHolder(@NonNull View itemView) {
            super(itemView);
            ivAvatar = itemView.findViewById(R.id.iv_avatar);
            tvName = itemView.findViewById(R.id.tv_name);
            tvId = itemView.findViewById(R.id.tv_id);
            tvBtn = itemView.findViewById(R.id.tv_btn);
        }
    }

    public static class ItemBean {

        public int itemType;

        public String avatarUrl;
        public String avatarPath;

        public String name;
        public String exhibition;
        public String id;
        public int relation;
        public String alias;

        public String groupOwner;

        public ItemBean(FriendTable friend) {
            itemType = ITEM_FRIEND;
            avatarUrl = friend.getFriendAvatar();
            avatarPath = friend.getFriendAvatarPath();
            name = friend.getFriendNickname();
            alias = friend.getFriendAlias();
            id = friend.getFriendUserId();
            exhibition = "ID:" + id;
            setRelation(ITEM_FRIEND, friend.getFriendUserId());
        }

        public ItemBean(RegisteredUserDto dto) {
            itemType = ITEM_FRIEND;
            avatarUrl = dto.getAvatar();
            name = dto.getNickname();
            id = dto.getUserId();
            exhibition = "ID:" + id;
            setRelation(ITEM_FRIEND, dto.getUserId());
        }

        public ItemBean(TouristsUserDto dto) {
            itemType = ITEM_FRIEND;
            avatarUrl = dto.getAvatar();
            name = dto.getNickname();
            id = dto.getTemporaryId();
            exhibition = "ID:" + id;
            setRelation(ITEM_FRIEND, dto.getTemporaryId());
        }

        public ItemBean(GroupDto dto) {
            itemType = ITEM_GROUP;
            avatarUrl = dto.getGroupAvatar();
            name = dto.getGroupName();
            id = dto.getGroupId();
            groupOwner = dto.getGroupOwnerUserId();
            exhibition = "ID:" + id;
            setRelation(ITEM_GROUP, dto.getGroupId());
        }

        public ItemBean(GroupTable group) {
            itemType = ITEM_GROUP;
            if (!TextUtils.isEmpty(group.getGroupAvatar())) {
                avatarUrl = group.getGroupAvatar();
            } else {
                String[] v = GroupMemberTable.getAvatarEntityLoc(group.getGroupId());
                if (v != null) {
                    avatarUrl = v[1];
                    avatarPath = v[2];
                }
            }
            name = group.getGroupName();
            id = group.getGroupId();
            groupOwner = group.getGroupOwnerId();
            exhibition = "ID:" + id;
            setRelation(ITEM_GROUP, group.getGroupId());
        }

        public ItemBean(GroupMemberTable groupMember, String keyword) {
            itemType = ITEM_GROUP;
            String[] v = GroupMemberTable.getAvatarEntityLoc(groupMember.getGroupId());
            if (v != null) {
                avatarUrl = v[1];
                avatarPath = v[2];
            }
            name = groupMember.getGroupName();
            id = groupMember.getGroupId();
            alias = groupMember.getGroupMemberAlias();
            String temp;
            if (groupMember.getGroupMemberName().contains(keyword)) {
                temp = groupMember.getGroupMemberName();
            } else if (groupMember.getGroupMemberAlias().contains(keyword)) {
                temp = groupMember.getGroupMemberAlias();
            } else {
                if (!TextUtils.isEmpty(groupMember.getGroupMemberAlias()))
                    temp = groupMember.getGroupMemberAlias() + "(" + groupMember.getGroupId() + ")";
                else
                    temp = groupMember.getGroupMemberName() + "(" + groupMember.getGroupId() + ")";
            }
            exhibition = "包含:" + temp;
            setRelation(ITEM_GROUP, groupMember.getGroupId());
        }

        public SearchBean toFriend(String keyword) {
            SearchBean s = new SearchBean();
            s.setType(SearchBean.FRIEND);
            s.setUserId(id);
            s.setName(name);
            s.setAlias(alias);
            s.setAvatars(avatarUrl);
            s.setContent(keyword);
            if (relation == RELATION_SELF)
                s.setStatus(SearchBean.STA_SELF);
            else if (relation == RELATION_FRIENDLY)
                s.setStatus(SearchBean.STA_FRIENDLY);
            else
                s.setStatus(SearchBean.STA_SEARCH);
            return s;
        }

        public SearchBean toGroup(String keyword) {
            SearchBean s = new SearchBean();
            s.setType(SearchBean.GROUP);
            s.setGroupId(id);
            s.setAvatars(avatarUrl);
            s.setName(name);
            s.setAlias(alias);
            s.setContent(keyword);
            s.setGroupOwnerId(groupOwner);
            if (relation == RELATION_FRIENDLY)
                s.setStatus(SearchBean.STA_FRIENDLY);
            else
                s.setStatus(SearchBean.STA_SEARCH);
            return s;
        }

        void setRelation(int itemType, String id) {
            if (itemType == ITEM_FRIEND) {
                if (UserPreferences.getUserId().equals(id))
                    relation = RELATION_SELF;
                else if (FriendTable.getTableIdByFriendUserId(id) > 0L)
                    relation = RELATION_FRIENDLY;
                else
                    relation = RELATION_UNFAMILIAR;
            } else {
                if (GroupTable.queryRowByGroupId(id) != null)
                    relation = RELATION_FRIENDLY;
                else
                    relation = RELATION_UNFAMILIAR;
            }
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, ItemBean item, int position);
    }
}
